{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导出.xlsx文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置文件导出路径**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =0 )\n",
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档01.xlsx\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置Sheet名称**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档02.xlsx\",\n",
    "            sheet_name =\"测试\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档03.xlsx\",\n",
    "            index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置要导出的列**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =0 )\n",
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档04.xlsx\",\n",
    "            sheet_name = \"测试文档\",\n",
    "            index=False,columns = [\"用户ID\",\"7月销量\",\"8月销量\",\"9月销量\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置编码格式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档05.xlsx\",\n",
    "           sheet_name = \"测试文档\",\n",
    "           index = False,\n",
    "           encoding = \"utf-8\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**缺失值处理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =2)\n",
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档06.xlsx\",\n",
    "           sheet_name=\"测试文档\",\n",
    "           index = False,\n",
    "           encoding = \"utf-8\",\n",
    "           na_rep = 0 #缺失值填充为0\n",
    "           )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**无穷值处理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =1)\n",
    "df.to_excel(excel_writer = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档07.xlsx\",\n",
    "           sheet_name = \"测试文档\",\n",
    "           index = False,\n",
    "           encoding = \"utf-8\",\n",
    "           na_rep = 0,\n",
    "           inf_rep = 0 #无穷值填充为0\n",
    "           )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导出为 .csv文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置文件导出路径**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =0)\n",
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档01.csv\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档02.csv\",\n",
    "          index = False )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置导出的列**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档03.csv\" ,\n",
    "          index= False,\n",
    "          columns = [\"用户ID\",\"7月销量\",\"8月销量\",\"9月销量\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置分隔符号**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档04.csv\" ,\n",
    "          index= False,\n",
    "          columns = [\"用户ID\",\"7月销量\",\"8月销量\",\"9月销量\"],\n",
    "          sep=\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**缺失值处理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =2)\n",
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档05.csv\" ,\n",
    "          index= False,\n",
    "          columns = [\"用户ID\",\"7月销量\",\"8月销量\",\"9月销量\"],\n",
    "          sep=\",\",\n",
    "          na_rep = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**设置编码格式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =2)\n",
    "df.to_csv(path_or_buf = r\"C:\\Users\\Administrator\\Excel-Python\\Data\\测试文档06.csv\" ,\n",
    "          index= False,\n",
    "          columns = [\"用户ID\",\"7月销量\",\"8月销量\",\"9月销量\"],\n",
    "          sep=\",\",\n",
    "          na_rep = 0,\n",
    "          encoding = \"gbk\" #设置为gbk或者utf-8-sig\n",
    "         )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将文件导出到多个Sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =0)\n",
    "df2 = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =1)\n",
    "df3 = pd.read_excel(r\"../Data/Chapter12.xlsx\",sheet_name =2)\n",
    "#声明一个对象\n",
    "writer = pd.ExcelWriter(r\"C:\\Users\\Administrator\\Excel-Python\\Data\\test02.xlsx\",\n",
    "                        engine = \"xlsxwriter\")\n",
    "#将df1、df2、df3写入Excel中的sheet1、sheet2、sheet3\n",
    "#重命名表1、表2、表3\n",
    "df1.to_excel(writer,sheet_name =\"表1\")\n",
    "df2.to_excel(writer,sheet_name =\"表2\")\n",
    "df3.to_excel(writer,sheet_name =\"表3\")\n",
    "#保存读写的内容\n",
    "writer.save()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "第12章 结果导出",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
